package cn.tedu.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

//代表Socket的服务器端--利用ServerSocket
public class Server {
    public static void main(String[] args) throws IOException {
        //触发含参构造(端口号0-65535)
        ServerSocket serverSocket=new ServerSocket(10086);
        System.out.println("服务器已开启~");
        //接受客户端发来的链接请求
        Socket socket = serverSocket.accept();
        System.out.println("服务器接收到了一个连接请求");
        //读取客户端发来的数据
            InputStream inputStream = socket.getInputStream();
            for (int i = 0; i < 10; i++) {
                int read = inputStream.read();
                char c=(char)read;
                System.out.print(c);
            }
            //给客户端写出数据
        OutputStream outputStream = socket.getOutputStream();
        String out=new Scanner(System.in).nextLine();
        outputStream.write(out.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
    }
}
